﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Collections;

namespace Sbook.AdminIstrator
{
    public partial class DeliveryDetail_Print : System.Web.UI.Page
    {
       
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Data.Mysession.isLogin)
                {
                    if (Request.QueryString["DeliveryId"] != null)
                    {
                        ArrayList lstUom = (ArrayList)Session["Uom"];
                        string Id = Request.QueryString["DeliveryId"];
                        List<Data.DeliveryDetailInfo> list = Business.DeliveryDetailService.DeliveryDetail_GetByTop("", "dt.DeliveryId=" + Id, "");
                        rpDeliveryDetail.DataSource = list;
                        rpDeliveryDetail.DataBind();

                        int sumQty = 0;
                        int sumPrice = 0;
                        for (int i = 0; i < rpDeliveryDetail.Items.Count; i++)
                        {
                            Label lblPrice = (Label)rpDeliveryDetail.Items[i].FindControl("lblPrice");
                            lblPrice.Text = Format_Price(lblPrice.Text.Replace(".0000", ""));
                            Label lblQty = (Label)rpDeliveryDetail.Items[i].FindControl("lblQty");
                            Label lblTotalPrice = (Label)rpDeliveryDetail.Items[i].FindControl("lblTotalPrice");
                            sumQty += Convert.ToInt32(lblQty.Text);
                            lblTotalPrice.Text = Format_Price((Convert.ToInt32(lblPrice.Text.Replace(".", "")) * Convert.ToInt32(lblQty.Text)).ToString());
                            if (lblTotalPrice.Text.Replace(".0000", "") != "")
                            {
                                sumPrice += Convert.ToInt32(lblTotalPrice.Text.Replace(".", ""));
                            }
                            //lblTotalPrice.Text = Format_Price(lblTotalPrice.Text.Replace(".0000", ""));
                            Label lblUom = (Label)rpDeliveryDetail.Items[i].FindControl("lblUom");
                            lblUom.Text = lstUom[i].ToString();
                        }

                        ((Label)rpDeliveryDetail.Controls[rpDeliveryDetail.Controls.Count - 1].Controls[0].FindControl("lblSumQty")).Text = sumQty.ToString();
                        ((Label)rpDeliveryDetail.Controls[rpDeliveryDetail.Controls.Count - 1].Controls[0].FindControl("lblSumPrice")).Text = Format_Price(sumPrice.ToString());

                        lblShippingPoint.Text = Business.DeliveryService.Delivery_GetByTop("", "DeliveryId=" + Id, "")[0].DeliveryShippingPoint;
                        lblDeliveryTime.Text = Business.DeliveryService.Delivery_GetByTop("", "DeliveryId=" + Id, "")[0].DeliveryDate;

                        if (Business.DeliveryService.Delivery_GetByTop("", "DeliveryId=" + Id, "")[0].UId != "")
                        {
                            List<Data.UserInfo> lstUs = Business.UserService.User_GetById(Business.DeliveryService.Delivery_GetByTop("", "DeliveryId=" + Id, "")[0].UId);
                            lblCustomerName.Text = lstUs[0].Fullname;
                            lblAddress.Text = lstUs[0].Address;
                            lblClass.Text = lstUs[0].Class;
                            lblUniversity.Text = lstUs[0].University;
                            lblEmail.Text = lstUs[0].Email;
                            lblPhone.Text = lstUs[0].Phone;
                        }
                        else
                        {
                            lblCustomerName.Text = Business.DeliveryService.Delivery_GetByTop("", "DeliveryId=" + Id, "")[0].DeliveryName;
                            lblAddress.Text = Business.DeliveryService.Delivery_GetByTop("", "DeliveryId=" + Id, "")[0].DeliveryAdd;
                            lblClass.Text = Business.DeliveryService.Delivery_GetByTop("", "DeliveryId=" + Id, "")[0].DeliveryClass;
                            lblUniversity.Text = Business.DeliveryService.Delivery_GetByTop("", "DeliveryId=" + Id, "")[0].DeliveryUniversity;
                            lblEmail.Text = Business.DeliveryService.Delivery_GetByTop("", "DeliveryId=" + Id, "")[0].DeliveryEmail;
                            lblPhone.Text = Business.DeliveryService.Delivery_GetByTop("", "DeliveryId=" + Id, "")[0].DeliveryPhone;
                        }
                    }
                }
                else
                    Response.Redirect("/AdminIstrator/Login.aspx");
            }
        }

        protected void ltbExport_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < rpDeliveryDetail.Items.Count; i++)
            {

                Label lblPrice = (Label)rpDeliveryDetail.Items[i].FindControl("lblPrice");
                lblPrice.Text = lblPrice.Text.Replace(".", ",");
                Label lblQty = (Label)rpDeliveryDetail.Items[i].FindControl("lblQty");
                Label lblTotalPrice = (Label)rpDeliveryDetail.Items[i].FindControl("lblTotalPrice");

                lblTotalPrice.Text = lblTotalPrice.Text.Replace(".", ",");
            }


            ((Label)rpDeliveryDetail.Controls[rpDeliveryDetail.Controls.Count - 1].Controls[0].FindControl("lblSumPrice")).Text = ((Label)rpDeliveryDetail.Controls[rpDeliveryDetail.Controls.Count - 1].Controls[0].FindControl("lblSumPrice")).Text.Replace(".", ",");

            ltbExport.Visible = false;
            Response.ContentType = "application/vnd.ms-excel";
            Response.ContentEncoding = System.Text.UnicodeEncoding.UTF8;
            Response.AddHeader("Content-Disposition", "attachment; filename=Baocao.xls");
            StringWriter stringWriter = new StringWriter(); //System.IO namespace should be used
            HtmlTextWriter htmlTextWriter = new HtmlTextWriter(stringWriter);
            htmlTextWriter.WriteLine("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">");
            this.RenderControl(htmlTextWriter);
            Response.Write(stringWriter.ToString());
            Response.End();
        }

        protected string Format_Price(string Price)
        {
            Price = Price.Replace(".", "");
            Price = Price.Replace(",", "");
            string tmp = "";
            while (Price.Length > 3)
            {
                tmp = "." + Price.Substring(Price.Length - 3) + tmp;
                Price = Price.Substring(0, Price.Length - 3);
            }
            tmp = Price + tmp;
            return tmp;
        }
    }
}